$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'course/acstatisticsuser/detailList',
        datatype: "json",
		mtype: "POST",
        colModel: [			
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 120, hidden: false, width: 200, formatter:formatHref}, 			
			{ label: '第几次重看', name: 'revisitTimes', index: 'revisitTimes', width: 120,  width: 200}, 			
			{ label: '课程编号', name: 'courseCode', index: 'courseCode', formatter: formatterCode}, 			
			{ label: '是否完课', name: 'courseFinish', index: 'courseFinish',formatter:formatStatus }, 			
			{ label: '选项记录', name: 'itemRecord', index: 'itemRecord', width: 200,}, 			
			{ label: '最终环节', name: 'lastNodeName', index: 'lastNodeName',formatter: function(params) {
				return params == '结束'? '作业页': params == null ? '' : params
			}},					
			{ label: '学习时长', name: 'studayTime', index: 'studayTime',formatter: formatterTime}, 			
			{ label: '作业评分', name: 'homeworkScore', index: 'homeworkScore',}, 			
			{ label: '知识总结评分', name: 'knowledgeSummaryScore',index: 'knowledgeSummaryScore' }, 			
			{ label: '愉悦度评分', name: 'pleasureScore', index: 'pleasureScore', formatter:formatterScore }, 			
			{ label: '投入度评分', name: 'engagementScore', index: 'engagementScore' , formatter:formatterEnga}, 			
			{ label: '完成作业的信心评分', name: 'confidenceScore', index: 'confidenceScore', formatter:formatterCoinf}, 			
			{ label: '创建时间', name: 'createTime',index: 'create_time' }, 			
        ],
		postData:{
            // 'courseId': window.location.href.slice(-18, -17) == 'i' ? '' : window.location.href.slice(-18, -17),
			'mobile': window.location.href.slice(-12, -1) == 'rdetail.htm' ? '' : window.location.href.slice(-12, -1)
        },
		shrinkToFit: false,
		viewrecords: true,
        height: 385,
        rowNum: 50,
		auto: false,
		autoScroll: true,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	// $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

function formatterScore(key,v,e) {
	if (e.pleasureScore != null) {
		return e.pleasureScore / 10
	}else {
		return ''
	}
}

function formatterEnga(key,v,e) {
	if (e.engagementScore != null) {
		return e.engagementScore / 10
	}else {
		return ''
	}
}

function formatterCoinf(key,v,e) {
	if (e.confidenceScore != null) {
		return e.confidenceScore / 10
	}else {
		return ''
	}
}

function  formatStatus(item){
	if(item ==1) {
		return '是';
	}
	 else {
	 return '否'  
	}
}
function  formatHref(key) {
	return key != null ? "<span>"+key+"</span>" : ''
}
function formatterCode(key) {
	if (key == 0) {
		return '第一课'
	}else if (key == 1) {
		return '第二课'
	}else if (key == 2) {
		return '第三课'
	}else if (key == 3) {
		return '第四课'
	}else if (key == 4) {
		return '第五课'
	}else if (key == 5) {
		return '第六课'
	}else if (key == 6) {
		return '第七课'
	}
}
function getTime(time) {
	let h = parseInt(time / 60 / 60 % 24)
	h = h < 10 ? '0' + h : h
	let m = parseInt(time / 60 % 60)
	 m = m < 10 ? '0' + m : m
	let s = parseInt(time % 60)
	 s = s < 10 ? '0' + s : s
	return [h, m, s]
}
function formatterTime(params) {
	if (params != undefined) {
		let time = getTime(params);
		return `${time[0]}时${time[1]}分${time[2]}秒`
	}else {
		return ''
	}
}

var vm = new Vue({
	el:'#app',
	data:{
		showList: true,
		isHidden: false,
		title: null,
		acStatisticsUser: {},
		gridSearch: '',
		kindList: [
			{
				id: 1,
				name: '第一课'
			},
			{
				id: 2,
				name: '第二课'
			},
			{
				id: 3,
				name: '第三课'
			},
			{
				id: 4,
				name: '第四课'
			},
			{
				id: 5,
				name: '第五课'
			},
			{
				id: 6,
				name: '第六课'
			},
			{
				id: 7,
				name: '第七课'
			},
		],
		searchTell: '',
		mobile: ''
},
	methods: {
		onTell(e) {
				this.searchTell = e;
				$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'mobile': this.searchTell,
                },
				page: 1
				}).trigger('reloadGrid');
			if (e == '') {
				$("#jqGrid").setGridParam().hideCol("courseCode").trigger("reloadGrid");
			}else {
				$("#jqGrid").setGridParam().showCol("courseCode").trigger("reloadGrid");
			}
		},
		onSelect() {
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'courseId': this.gridSearch
                },
				page: 1,
            }).trigger('reloadGrid');
			if (this.gridSearch == '') {
				$("#jqGrid").setGridParam().hideCol("mobile").trigger("reloadGrid");
			}else {
				$("#jqGrid").setGridParam().showCol("mobile").trigger("reloadGrid");
			}

		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.acStatisticsUser = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.acStatisticsUser.id == null ? "course/acstatisticsuser/save" : "course/acstatisticsuser/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.acStatisticsUser),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "course/acstatisticsuser/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "course/acstatisticsuser/info/"+id, function(r){
                vm.acStatisticsUser = r.acStatisticsUser;
            });
		},
		exportExcel: function (event) {
			window.location.href = baseURL +"course/acstatisticsuser/exportExcel";
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	},
	mounted() {
		this.searchTell = window.location.href.slice(-12, -1) == 'rdetail.htm' ? '' : window.location.href.slice(-12, -1)
		}
});